﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace AlimentadorGenerator
{
    [Serializable()]
    public class RegisterCode
    {
        private const string ALPHABET = "ABCDEFGHIJKLMNOPRSTUVWXYZ0123456789";
        private const int offset1 = 1;
        private const int offset2 = 1;
        private const int offset3 = 1;

        private string m_requestCode;
        private string m_responseCode;

        /// <summary>
        /// Constructor
        /// </summary>
        public RegisterCode()
        {
            m_requestCode = "-999";
            m_responseCode = "-1";
        }

        /// <summary>
        /// Returns the request code
        /// </summary>
        public string GetRequestCode
        {
            get
            {
                return m_requestCode;
            }
        }

        /// <summary>
        /// Returns the response code
        /// </summary>
        public string GetResponseCode
        {
            get
            {
                return m_responseCode;
            }
        }

        /// <summary>
        /// Calculates the 3 code components and return the request string
        /// </summary>
        /// <returns>bool</returns>
        public bool RequestCode()
        {
            string code1 = "", code2 = "", code3 = "";

            try
            {
                Random random = new Random();

                for (int i = 0; i < 4; i++)
                {
                    int randomNumber = random.Next(1, ALPHABET.Length);
                    code1 += ALPHABET[randomNumber];
                }

                for (int i = 0; i < 4; i++)
                {
                    int randomNumber = random.Next(1, ALPHABET.Length);
                    code2 += ALPHABET[randomNumber];
                }

                for (int i = 0; i < 4; i++)
                {
                    int randomNumber = random.Next(1, ALPHABET.Length);
                    code3 += ALPHABET[randomNumber];
                }

                m_requestCode = code1 + "-" + code2 + "-" + code3;
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Calculate the return code according to RequestCode
        /// </summary>
        /// <returns>bool</returns>
        public bool ResponseCode(string testCode, int maxChar = 4)
        {
            if (testCode != null && testCode.Length == 14)
            {
                string code1 = "", code2 = "", code3 = "";

                for (int i = 0; i < maxChar; i++)
                {
                    char c = testCode[i];
                    int pos = ALPHABET.IndexOf(c);

                    int max = pos + offset1;
                    if (max >= ALPHABET.Length)
                    {
                        max -= ALPHABET.Length;
                    }

                    code1 += ALPHABET[max];
                }

                for (int i = 0; i < maxChar; i++)
                {
                    char c = testCode[i + maxChar + 1];
                    int pos = ALPHABET.IndexOf(c);
                    int max = pos + offset2;
                    if (max >= ALPHABET.Length)
                    {
                        max -= ALPHABET.Length;
                    }
                    code2 += ALPHABET[max];
                }

                for (int i = 0; i < maxChar; i++)
                {
                    char c = testCode[i + maxChar * 2 + 2];
                    int pos = ALPHABET.IndexOf(c);
                    int max = pos + offset3;
                    if (max >= ALPHABET.Length)
                    {
                        max -= ALPHABET.Length;
                    }
                    code3 += ALPHABET[max];
                }

                m_responseCode = code1 + "-" + code2 + "-" + code3;
                return true;
            }

            return false;
        }
    }
}
